{extend name="layout/base"}
{block name='content'}
<body>
<div class="layui-fluid">
    <div class="layui-form">
        <div class="layui-col-md6">
            <input type="hidden" name="aid" value="{$data.aid}">
            <div class="layui-form-item">
                <label class="layui-form-label">文章标题：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="title" placeholder="请输入文章标题" lay-verify="required" value="{$data.title}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">选择栏目：</label>
                <div class="layui-input-block">
                    <select name="cid" id="cateid" class="layui-select" lay-filter="cate" data-cid = {$data.cid}>
                        {foreach $type as $key=>$value }
                        <option value="{$value.cate_id}" {if $data.cid == $value.cate_id}selected{/if}>{$value.html}{$value.cate_name}
                        </option><br/>
                        {/foreach}
                    </select>
                </div>
            </div>

            <div class="layui-form-item" id="endtime" style="display: none">
                <label class="layui-form-label">活动结束时间：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="endtime" id="test1" value="{$data.endtime}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">缩略图：</label>
                <div class="layui-input-block">
                    <div id="defultimage">
                        <div class="public_edit_image">
                            <img src="{$data.image}" alt="" width="80px" height="80px"/>
                            <span class="close" id="delimage"></span>
                            <input type="hidden" value="{$data.image}" id="imageval">
                        </div>
                    </div>
                    <div id="photo" data-id="{$data.image}"></div>
                    <button type="button" class="layui-btn" id="files">
                        <i class="layui-icon">&#xe67c;</i>修改图片
                    </button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">概要：</label>
                <div class="layui-input-block">
                    <textarea name="brief" cols="30" rows="10">{$data.brief}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">来源：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="source" placeholder="请输入文章来源" value="{$data.source}">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容：</label>
            <div class="layui-input-block">
                <textarea id="Content" cols="30" rows="10">
                    {$data.content}
                </textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
            </div>
        </div>
    </div>
</div>
</div>
</body>
<script>
    var Content = UE.getEditor('Content');
    layui.use(['jquery', 'form', 'layer','laydate','colorpicker','upload'], function ($, form, layer,laydate,colorpicker, upload) {
        var cateid = $("#cateid").data("cid");
        if (cateid == 5) {
            $('#endtime').show()
        } else {
            $('#endtime').hide()
        }
        $('#delimage').click(function () {
            var path = $('#imageval').val();
            $.ajax({
                url: "{:url('index/delImg')}",
                data: {path: path},
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg);
                        $('#defultimage').empty();
                        uploadPic()
                    }
                }
            })
        })

        var imgIsNull = $('.public_edit_image img').attr('src')
        if (imgIsNull == '') {
            $('#defultimage').empty();
            uploadPic()

        } else {
            $('#files').click(function () {
                var path = $('#defultimage').html()
                if (path != '') {
                    layer.msg('请先删除原图')
                    return false
                }
            })
        }
        function uploadPic () {
            var uploadInst = upload.render({
                elem: '#files' //绑定元素
                ,url: '{:url(\'Upload/upfile\')}' //上传接口
                ,data: {isThumb: 1} //生成缩略图
                ,done: function(res){
                    var editPath = res.data[0]
                    // $('#files').hide();
                    $('#photo').html('<img src="' + res.data + '" alt="" width="80px" height="80px"/><span class="close" id="del"></span>');
                    $("#photo").attr("data-id",res.data);
                    // 上传图片删除
                    $('#photo #del').click(function () {
                        $.ajax({
                            type: 'post',
                            url: "{:url('index/delImg')}",
                            data: {path: editPath},
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg)
                                    $('#photo').empty();
                                    parent.location.reload();
                                } else {
                                    layer.msg(res.msg)
                                }
                            }
                        })
                    })
                }
                ,error: function(){
                    layer.msg('上传异常', {icon: 5});
                }
            });
        };
        laydate.render({
            elem:'#test1'
        })

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            ,color: '#1c97f5'
            ,done: function(color){
                $('#test-form-input').val(color);
            }
        });

        form.on('select(cate)', function(data){
            if (data.value == 5) {
                $('#endtime').show()
            } else {
                $('#endtime').hide()
            }
        });

        form.on('submit(submit)', function (obj) {
            delete obj.field.file;

            var content = Content.getContent();
            var imgPath =  $("#photo").data("id");
            if(imgPath){
                obj.field.image = imgPath;
            }
            if(content){
                obj.field.content = content;

            }
            $.ajax({
                type: 'post',
                url: "{:url('article/edit')}",
                data: obj.field,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 6});
                        location.href="{:url('article/lists')}";
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function () {
                    layer.msg('请求错误', {icon: 5});
                }
            })
        });
    });
</script>
{/block}